import pygame
import sys
from settings import Settings
from myball import Myball
from game_stats import GameStats
from button import Button
from badball import Badball
import game_functions as gf
from pygame.sprite import Group
def run_game():
    pygame.init()
    ai_settings=Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("玩个球啊！")

    play_button = Button(ai_settings, screen, "Play")
    
    stats = GameStats(ai_settings)
    #创建我的球
    myball = Myball(ai_settings,screen)
    #创建一个存炮弹的编组
    bullets = Group()
    #创建一个敌人编组
    badballs = Group()
    #创建敌星人群
    gf.create_fleet(ai_settings, screen, myball, badballs)
    
    
    while True:
        gf.check_events(ai_settings, screen, stats, play_button, myball, badballs, bullets)
        
        if stats.game_active:
            myball.update()
            gf.update_bullets(ai_settings, screen, myball, badballs, bullets)
            gf.update_badballs(ai_settings, stats, screen, myball, badballs, bullets)   
            ##删除已消失子弹
            #for bullet in bullets.copy():
            #if bullet.rect.bottom <= 0:
                #bullets.remove(bullet)
            #print(len(bullets))
        
        gf.update_screen(ai_settings, screen, stats, myball, badballs, bullets, play_button)
        
                

        
        #screen.fill(ai_settings.bg_color)
        #myball.blitme()
        #pygame.display.flip()
run_game()

